package com.zhz.mall.admin.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description: oss配置文件
 * @motto: 代码源于生活，高于生活艺术
 * @author: zhouhengzhe
 * @date: 2023/1/13 11:23
 * @since 1.0
 **/
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssConfigProperties {

    private String endpoint;

    private String accessKeyId;

    private String accessKeySecret;

    private String bucketName;

    private Policy policy;

    private Long maxSize;

    private Dir dir;

    private final static String URL_PREFIX = "http://";

    private final static String URL_SPLIT = ".";
    @Data
    public static class Policy {
        private Long expire;

        public Date getExpiration() {
            return new Date(System.currentTimeMillis() + expire * 1000);
        }
    }

    @Data
    public static class Dir {
        private String prefix;

        public String getPath() {
            // 存储目录
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            return prefix + sdf.format(new Date());
        }
    }

    public String getAction() {
        return URL_PREFIX + this.bucketName + URL_SPLIT + this.endpoint;
    }

    public Long getMaxSize() {
        return this.maxSize * 1024 * 1024;
    }

}
